﻿using CJAtech.BBS.DataEntity;
using CJAtech.BBS.Process;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bigger.Framework;
using Bigger.Qiniu;
using Bigger.Base.Cache;
using CJAtech.BBS.Business;

namespace CJAtech.BBS.Web.Controllers
{
    [BBSConfigFilter("lmsbbs")]
    public class LMSBBSController : BBSBaseController
    {
        public override BBSBaseProcess CreateBBSProcess()
        {
            return new LMSBBSProcess("lmsbbs");
        }

        public ActionResult Category(int? id)
        {
            if (!id.HasValue || id.Value <= 0)
            {
                return Http404();
            }
            var objCommunityConfig = CurrentBBSProcess.CurrentCommunityConfig;
            BizCategoryInfo objBizCategoryInfo = new BizCategoryInfo(objCommunityConfig.ConnectionStringKey, objCommunityConfig.DataObjectParam);
            CategoryInfo objCategoryInfo = objBizCategoryInfo.GetRecordCache("", id.Value, TimeSpan.FromSeconds(5));
            if (objCategoryInfo == null)
            {
                return Http404();
            }
            return CheckView(objCategoryInfo);
        }

        public ActionResult Article(int? id)
        {
            if (!id.HasValue || id.Value <= 0)
            {
                return Http404();
            }
            CommunityConfig objCommunityConfig = CurrentBBSProcess.CurrentCommunityConfig;
            ArticleInfoProcess objArticleInfoProcess = new ArticleInfoProcess(objCommunityConfig.ConnectionStringKey, objCommunityConfig.DataObjectParam);
            ArticleResultInfo objArticleResultInfo = objArticleInfoProcess.GetArticleResultInfo(id.Value, true, true);
            if (objArticleResultInfo == null || objArticleResultInfo.ArticleInfo == null)
            {
                return Http404();
            }
            if (objArticleResultInfo.ArticlePictureCollection.IsNoNull())
            {
                foreach (ArticlePicture objArticlePicture in objArticleResultInfo.ArticlePictureCollection)
                {
                    //objArticlePicture.PictureUrl = Bigger.Qiniu.QiniuImageHelper.CreateThumbnailFromQiniu(objArticlePicture.PictureUrl, 50, 80);
                    objArticlePicture.PictureUrl = objArticlePicture.PictureUrl.CreateThumbnailByWidth(800);
                }
            }
            this.CurrentBBSProcess.LoadArticleRelatedRecord(objArticleResultInfo.ArticleInfo);
            UserInfo objUserInfo = this.CurrentBBSProcess.GetUserInfo();
            if (objArticleResultInfo.ArticleInfo.IsRelease != 1 && !string.Equals(objArticleResultInfo.ArticleInfo.ObjectId, objUserInfo.UserId))
            {
                return Content("对不起，当前文章还在审核中");
            }
            ViewBag.UserInfo = objUserInfo;
            return CheckView(objArticleResultInfo);
        }

        public ActionResult Access()
        {
            //测试地址路径：/cjbbs/access/view-index 访问视图Index
            return CheckView();
        }
    }
}